package cn.kgc.controller.logic.desktop;

import cn.kgc.pojo.Schedule;
import cn.kgc.service.desktop.ScheduleDaoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.sql.Date;
import java.util.List;

@Controller("ScheduleController_LUORUI")
@RequestMapping("ScheduleBlock")
public class ScheduleController {


    @Autowired
    ScheduleDaoService scheduleDaoService;

    @RequestMapping("queryAllAffairs")
    @ResponseBody
    public List<Schedule> queryAllAffairs(@RequestParam(value = "currentPage", required = true) String currentPage,
                                          @RequestParam(value = "pageSize", required = true) String pageSize,
                                          @RequestParam(value = "keyword", required = false) String keyword,
                                          @RequestParam(value = "dateStart", required = false) String dateStart,
                                          @RequestParam(value = "dateEnd", required = false) String dateEnd,
                                          @RequestParam(value = "contact", required = false) String contactName,    // another chart
                                          @RequestParam(value = "type", required = false) String type,
                                          @RequestParam(value = "location", required = false) String location,
                                          @RequestParam(value = "userId", required = false) Integer userId) {// another chart

        System.out.println("User ID is" + userId);
        System.out.println("keyword is " + keyword);
        System.out.println("contact is " + contactName);
        System.out.println("type is" + type);
        System.out.println("location is" + location);
        System.out.println("dateEnd" + dateEnd);
        System.out.println("dateStart" + dateStart);
        Date dateStartR = null;
        Date dateEndR = null;
        if (keyword == "") {
            keyword = "";
        }
        if (dateStart == "") {
            dateStartR = null;
        } else {
            dateStartR = Date.valueOf(dateStart);
        }
        if (dateEnd == "") {
            dateEndR = null;
        } else {
            dateEndR = Date.valueOf(dateEnd);
        }
        if (contactName == "") {
            contactName = null;
        }
        if (type == "") {
            type = null;
        }
        if (location == "") {
            location = null;
        }
        System.out.println("------------------------------------------------------------------------");
        System.out.println("keyword is " + keyword);
        System.out.println("contact is" + contactName);
        System.out.println("type is" + type);
        System.out.println("location is" + location);
        System.out.println("dateEnd" + dateEndR);
        System.out.println("dateStart" + dateStartR);
        return scheduleDaoService.queryAllAffairs(keyword, dateStartR, dateEndR, contactName, type, location, Integer.valueOf(currentPage), Integer.valueOf(pageSize), userId);
    }

    @RequestMapping("queryAffairsCount")
    @ResponseBody
    public int queryAffairsCount(@RequestParam(value = "keyword", required = false) String keyword,
                                 @RequestParam(value = "dateStart", required = false) String dateStart,
                                 @RequestParam(value = "dateEnd", required = false) String dateEnd,
                                 @RequestParam(value = "contact", required = false) String contactName,    // another chart
                                 @RequestParam(value = "typeName01", required = false) String type,
                                 @RequestParam(value = "typeName02", required = false) String location,
                                 @RequestParam(value = "userId", required = false) String userId) {
        System.out.println("User ID is" + userId);
        System.out.println("keyword is " + keyword);
        System.out.println("contact");
        System.out.println("type is" + type);
        System.out.println("location is" + location);
        System.out.println("dateEnd" + dateEnd);
        System.out.println("dateStart" + dateStart);
        Date dateStartR = null;
        Date dateEndR = null;
        if (keyword == "" || keyword == null) {
            keyword = "";
        }
        if (dateStart == "" || dateStart == null) {
            dateStartR = null;
        } else {
            dateStartR = Date.valueOf(dateStart);
        }
        if (dateEnd == "" || dateEnd == null) {
            dateEndR = null;
        } else {
            dateEndR = Date.valueOf(dateEnd);
        }
        if (contactName == "") {
            contactName = null;
        }
        if (type == "") {
            type = null;
        }
        if (location == "") {
            location = null;
        }
        return scheduleDaoService.queryAffairsCount(keyword, dateStartR, dateEndR, contactName, type, location,Integer.valueOf(userId));
    }

    @RequestMapping("typeFor")
    @ResponseBody
    public List<String> queryAllTypeInfo() {
        return scheduleDaoService.queryAllTypeInfo();
    }


    @RequestMapping("contactFor")
    @ResponseBody
    public List<String> queryAllContactInfo() {
        return scheduleDaoService.queryAllContact();
    }


    @RequestMapping("locationFor")
    @ResponseBody
    public List<String> queryAllLocation() {
        return scheduleDaoService.queryAllLocationInfo();
    }

    @RequestMapping("addData")
    @ResponseBody
    public boolean addData(Schedule schedule) {
        return scheduleDaoService.addData(schedule);
    }

    @RequestMapping("queryAllDateFromAffairs")
    @ResponseBody
    public List<String> queryAllDateFromAffairs(@RequestParam("idR") String id) {
        return scheduleDaoService.queryAllDateFromAffairs(Integer.valueOf(id));
    }


    @RequestMapping("deleteData")
    @ResponseBody
    public boolean deleteData(@RequestParam("idR") int id) {
        return scheduleDaoService.deleteData(id);
    }

    @RequestMapping("queryUpdatedTarget")
    @ResponseBody
    public Schedule queryUpdatedTarget(@RequestParam("idR") int id, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("schedule", scheduleDaoService.queryUpdatedTarget(id));
        System.out.println("queryUpdatedTarget method is in");
        return scheduleDaoService.queryUpdatedTarget(id);
    }

    @RequestMapping(value = "updateScheduleData", method = RequestMethod.POST)
    public String updateData(@RequestParam("idR") int id, Schedule
            schedule) {
        System.out.println("method is in");
        System.out.println(schedule);
        scheduleDaoService.updateData(id, schedule);
//        JSONObject jsonObjectOfSchedule = JSONObject.parseObject(schedule);
//        Schedule scheduleTrans = JSON.toJavaObject(jsonObjectOfSchedule, Schedule.class);
//        System.out.println(scheduleTrans);
        /*this is an update function */
        /*After add achieved*/
        return "redirect:/to/schedule";
    }

    @RequestMapping(value = "updatePage")
    @ResponseBody
    public String updatePage() {
        return "/schedule/update";
    }

    @RequestMapping(value = "/schedule/update", method = RequestMethod.POST, produces = "application/json")
    public ModelAndView scheduleUpdate(@RequestParam("schedule") String schedule, Model model) {
        System.out.println("The schedule is " + schedule);
        JSONObject jsonObjectOfSchedule = JSONObject.parseObject(schedule);
        Schedule schedule1Trans = JSON.toJavaObject(jsonObjectOfSchedule, Schedule.class);
        System.out.println(schedule1Trans);
        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("schedule", schedule1Trans);
        model.addAttribute("type", scheduleDaoService.queryAllTypeInfo());
        model.addAttribute("location", scheduleDaoService.queryAllLocationInfo());
        model.addAttribute("user", scheduleDaoService.queryAllContact());
        model.addAttribute("dict", scheduleDaoService.queryAllDict());
        modelAndView.setViewName("/schedule/update");
        return modelAndView;
    }

    @RequestMapping("queryLeaveRequest")
    @ResponseBody
    public List<Schedule> queryLeaveRequest(@RequestParam("idR") String id){
        return scheduleDaoService.queryLeaveRequest(Integer.valueOf(id));
    }

    @RequestMapping("queryLeaveRequestCount")
    @ResponseBody
    public int queryLeaveRequestCount(@RequestParam("idR")String id){
        return scheduleDaoService.queryLeaveRequestCount(Integer.valueOf(id));
    }

    @RequestMapping("queryLeaveApplyCount")
    @ResponseBody
    public int queryLeaveApplyCount(@RequestParam("idR") String id){
        return scheduleDaoService.queryLeaveApplyCount(Integer.valueOf(id));
    }
}
